package com.ewzj.bigdata.mvp.model;

import android.app.Application;

import com.ewzj.bigdata.app.GlobalHttpHandlerImpl;
import com.ewzj.bigdata.app.utils.Apputils;
import com.ewzj.bigdata.mvp.model.api.Api;
import com.ewzj.bigdata.mvp.model.api.service.CommonService;
import com.ewzj.bigdata.mvp.model.entity.reqbeans.ReqIdBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.BannerBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseData;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseJson;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseListData;
import com.ewzj.bigdata.mvp.model.entity.resbean.FilterBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.NewBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.ResBannerData;
import com.ewzj.bigdata.mvp.model.entity.resbean.TabBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import com.jess.arms.di.scope.ActivityScope;

import javax.inject.Inject;

import com.ewzj.bigdata.mvp.contract.CommonContract;

import java.lang.reflect.Type;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.functions.Function;
import okhttp3.RequestBody;


@ActivityScope
public class CommonModel extends BaseModel implements CommonContract.Model {
    private Gson mGson;
    private Application mApplication;
    private CommonService service;
    @Inject
    public CommonModel(IRepositoryManager repositoryManager, Gson gson, Application application) {
        super(repositoryManager);
        this.mGson = gson;
        this.mApplication = application;
        this.service=repositoryManager.obtainRetrofitService(CommonService.class);
    }

    @Override
    public Observable<List<NewBean>> getnews(RequestBody requestBody) {
        return service.getNews(requestBody).
                map(new Function<BaseData<BaseListData<NewBean>>, List<NewBean>>() {
                    @Override
                    public List<NewBean> apply(BaseData<BaseListData<NewBean>> baseListDataBaseData) throws Exception {
                        return baseListDataBaseData.getData().getList();
                    }
                });

    }

    @Override
    public Observable<List<BannerBean>> getBanners(String id) {
        ReqIdBean reqIdBean=new ReqIdBean(id);
        return service.getBanners(Apputils.json2Body(mGson.toJson(reqIdBean)))
                .map(new Function<BaseData<List<BannerBean>>, List<BannerBean>>() {
                    @Override
                    public List<BannerBean> apply(BaseData<List<BannerBean>> listBaseData) throws Exception {
                        return listBaseData.getData();
                    }
                });
    }

    @Override
    public Observable<List<FilterBean>> getFilterdatas(String id) {
        ReqIdBean reqIdBean=new ReqIdBean(id);
        return service.getFilters(Apputils.json2Body(mGson.toJson(reqIdBean)))
                .map(new Function<BaseData<List<FilterBean>>, List<FilterBean>>() {
                    @Override
                    public List<FilterBean> apply(BaseData<List<FilterBean>> listBaseData) throws Exception {
                        return listBaseData.getData();
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
    }

}